package com.hoangkien.DAO;

import java.util.ArrayList;

import com.hoangkien.entity.ImageEntity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ImageDAO {
	private static final String TAG = "ImageDAO";
	private final SQLiteDatabase db; 
	
	public ImageDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}
	
	public long insert_image(ImageEntity imageEntity){
		ContentValues contentValues = new ContentValues();
		contentValues.put(ImageScheme.IMAGE_ID, imageEntity.getId());
		contentValues.put(ImageScheme.CATEGORY_ID, imageEntity.getCatID());
		contentValues.put(ImageScheme.IMAGE_NAME, imageEntity.getName());
		contentValues.put(ImageScheme.LINK_IMAGE_SMALL, imageEntity.getLinkImagesmall());
		contentValues.put(ImageScheme.LINK_IMAGE_LARGE, imageEntity.getLinkImagelarge());
		if(db.insert(ImageScheme.TABLE_IMAGE_NAME, null, contentValues) == -1){
			Log.e(TAG,"Bug insert image");
		} else {
			Log.e(TAG,"Successfully insert image");
		}
		return 0;
	}
	
	public ArrayList<ImageEntity> getAllImage(){
		ArrayList<ImageEntity> listImage = new ArrayList<ImageEntity>();
		String query = "select * from "+ImageScheme.TABLE_IMAGE_NAME;
		Cursor cursor = db.rawQuery(query, null);
		if(cursor.moveToFirst()) {
			do {
				ImageEntity imageEntity = new ImageEntity();
				imageEntity.setId(cursor.getInt(0));
				imageEntity.setCatID(cursor.getInt(1));
				imageEntity.setName(cursor.getString(2));
				imageEntity.setLinkImagesmall(cursor.getString(3));
				imageEntity.setLinkImagelarge(cursor.getString(4));
				listImage.add(imageEntity);
			} while (cursor.moveToNext());
		}
		return listImage;
	}
}
